﻿using System.ComponentModel;
using System.Diagnostics.Contracts;
using GIMS.Def.Arguments;
using GIMS.Def.Flags;
using GIMS.Def.Interfaces;

namespace GIMS.Def.Settings
{
    public abstract class Setting : IScriptPart
    {
        protected string name;
        protected string description;
        protected string instruction;
        protected Argument argument;
        protected SettingType type=SettingType.none;
        protected bool active;
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// returns the name of the Setting
        /// </summary>
        public string Name
        {
            get
            {
                Contract.Ensures(!string.IsNullOrEmpty(name));
                return name;
            }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public Argument Argument
        {
            get
            {
                Contract.Ensures(argument!=null);
                return argument;
            }
        }

        /// <summary>
        /// Active defines if the Setting is active or not.
        /// </summary>
        public bool Active
        {
            get { return active; }
            set
            {  
                active = value;
                OnPropertyChanged("active");
            }
        }

        public SettingType Type
        {
            get
            {
                Contract.Ensures(type!=Flags.SettingType.none);
                return type;
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// returns the command of the setting
        /// if the setting is not active an empty string is returned
        /// </summary>
        public virtual string CreateScriptPart()
        {
            if (active)
                return instruction + " " + argument.CreateScriptPart();
            else
                return "";
        }
    }
}
